import logging
import os
from datetime import datetime

loggers = {}


def createLogger(name):
    global loggers
    if loggers.get(name) and len(loggers[name].handlers) != 0:
        return loggers.get(name)
    else:
        if not os.path.exists('./logs'):
            os.mkdir('./logs')
        # 创建一个logger
        logger = logging.getLogger(name)
        logger.setLevel(logging.DEBUG)
        # 创建一个文件处理器，用于处理info级别的日志
        info_handler = logging.FileHandler(
            './logs/info_{}.log'.format(datetime.now().strftime('%Y_%m_%d_%H')), encoding="utf-8", mode="a")
        info_handler.setLevel(logging.INFO)

        # 创建一个格式化器
        formatter = logging.Formatter('%(asctime)s | %(module)s | %(message)s')

        # 为处理器添加格式化器
        info_handler.setFormatter(formatter)

        # 将处理器添加到logger
        logger.addHandler(info_handler)

    loggers[name] = logger

    return logger
